import pandas as pd
import requests

pre_data = None
pd_data = None
start_year = 2020
end_year = 2025

for year in range(start_year, end_year + 1):
    for i in range(1, 10**4):
        data = {
            'pageNum': i,
            'pageSize': 30,
            'column': 'szse',
            'tabName': 'fulltext',
            'plate': 'shmb',
            'category': 'category_ndbg_szsh',
            'seDate': f'{year}-03-18~{year+1}-03-18',
            'isHLtitle': True
        }
        ret = requests.post(
            'http://www.cninfo.com.cn/new/hisAnnouncement/query', data=data)
        ret_data = ret.json()
        if pre_data == ret_data:
            break
        pre_data = ret.json()
        print(ret_data)
        # print(json.dumps(ret.json()))
        # print(ret_data['announcements'])
        if pd_data is None and ret_data['announcements']:
            pd_data = pd.DataFrame(ret_data['announcements'])
        else:
            pd_data = pd.concat(
                [pd_data, pd.DataFrame(ret_data['announcements'])],
                ignore_index=True)

        if not ret_data['hasMore']:
            break
        # time.sleep(0.4)

pd_data.to_csv('企业年报.csv', index=None)
